package com.kun.demo01;

public class ThreadCreate02_race implements Runnable{

    public static String winner = "";

    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (checkWinner(i)){
                break;
            }
            if (Thread.currentThread().getName().equals("rabbit") && i==50 ){
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName() + " --> 第 " + i+ " 步");
        }
    }
    public boolean checkWinner(int step){
        if (step >= 100){
            winner = Thread.currentThread().getName();
            System.out.println("最后的冠军是："+winner);
            return true;
        }
        if (!winner.equals("")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        ThreadCreate02_race race1 = new ThreadCreate02_race();
        new Thread(race1, "rabbit").start();
        new Thread(race1, "turtle").start();
    }
}
